﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using OFB.BLL.ViewModel;
using OFB.Domain;

namespace OFB.BLL.Repository
{
    public class CourseTraineeBLL : ICourseTraineeBLL
    {
        private readonly CourseTraineeContext context;
        private readonly CourseContext coursecontext;
        public readonly TraineeContext traineecontext;

        public CourseTraineeBLL()
        {
            context = new CourseTraineeContext();
            coursecontext = new CourseContext();
            traineecontext = new TraineeContext();
        }

        public Course CourseSearch(int courseId, out bool isBlock)
        {
            IDataParameter para = context.CourseTraineeViewModel.BuildParameter("HasFeedbacked", DbType.Boolean, ParameterDirection.Output);
            isBlock = false;

            Course course = coursecontext.Course.Init("Course_Search",
                coursecontext.Course.BuildParameter("courseId", DbType.Int32, courseId),
                para)
                                                .Map()
                                                .Get();

            isBlock = Convert.ToBoolean(para.Value);
            return course;
        }

        public bool RemoveTrainee(int courseId, string traineeId)
        {
            IDataParameter para = context.CourseTraineeViewModel.BuildParameter("Valid", DbType.Boolean, ParameterDirection.Output);
            context.CourseTraineeViewModel.Execute("TraineeCourse_Remove",
                context.CourseTraineeViewModel.BuildParameter("CourseId", DbType.Int32, courseId),
                context.CourseTraineeViewModel.BuildParameter("TraineeID", DbType.String, traineeId),
                para);
            return Convert.ToBoolean(para.Value);
        }

        public bool AddTrainee(string traineeId, int courseId, string groupName)
        {
            IDataParameter para = context.CourseTraineeViewModel.BuildParameter("Valid", DbType.Boolean, ParameterDirection.Output);
            context.CourseTraineeViewModel.Execute("TraineeCourse_Add",
                context.CourseTraineeViewModel.BuildParameter("CourseId", DbType.Int32, courseId),
                context.CourseTraineeViewModel.BuildParameter("TraineeID", DbType.String, traineeId),
                context.CourseTraineeViewModel.BuildParameter("GroupName", DbType.String, groupName),
                para);
            return Convert.ToBoolean(para.Value);
        }

        public ICollection<Trainee> TraineeUnCourseFilter(int type, string search)
        {
            return traineecontext.Trainee.Init("TraineeUnCourse_Filter",
                traineecontext.Trainee.BuildParameter("Type", DbType.Int32, type),
                traineecontext.Trainee.BuildParameter("Search", DbType.String, search))
                                                .MapCollection<Trainee>()
                                                .GetCollection();
        }

        public ICollection<CourseTraineeViewModel> Filter(int courseId, int type, string search, string group)
        {
            return context.CourseTraineeViewModel.Init("TraineeCourse_Filter",
                context.CourseTraineeViewModel.BuildParameter("CourseId", DbType.Int32, courseId),
                context.CourseTraineeViewModel.BuildParameter("Type", DbType.Int32, type),
                context.CourseTraineeViewModel.BuildParameter("Search", DbType.String, search),
                context.CourseTraineeViewModel.BuildParameter("GroupName", DbType.String, group))
                                                .MapCollection<CourseTraineeViewModel>()
                                                .GetCollection();
        }
    }
}
